package com.jr.controller;

import com.google.gson.Gson;
import com.jr.entity.BlackList;
import com.jr.entity.Enterprise;
import com.jr.entity.Users;
import com.jr.pojo.*;
import com.jr.service.BlackListService;
import com.jr.service.EnterpriseService;
import com.jr.service.UserService;
import com.jr.service.WhiterListService;
import com.jr.serviceimpl.BlackListServiceImpl;
import com.jr.serviceimpl.EnterpriseServiceImpl;
import com.jr.serviceimpl.UserServiceImpl;
import com.jr.serviceimpl.WhiteListServiceImpl;
import util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;


@WebServlet("/black/*")
public class BlackController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        try {
            //1.获得客户端发送过来的请求地址
            String str = request.getRequestURI();
//        str.lastIndexOf("/");
            String methodname = str.substring(str.lastIndexOf("/") + 1);
            Class cla = BlackController.class;
            Method declaredMethod = cla.getDeclaredMethod(methodname, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    EnterpriseService enterpriseService = new EnterpriseServiceImpl();

    UserService userService = new UserServiceImpl();
    WhiterListService whiterListService = new WhiteListServiceImpl();
    BlackListService blackListService = new BlackListServiceImpl();


    //用来判断是否第一次登陆
    public void ajaxFindUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        UserEnterprise user = userService.select(account,password);
        if(user == null){
            Users users = userService.findUser(account,password);
            Gson gson1 = new Gson();
            response.getWriter().println(gson1.toJson(users));
        }else{
            Gson gson = new Gson();
            response.getWriter().println(gson.toJson(user));
        }
        /*ArrayList<User> list = new ArrayList<>();
        list.add(user);*/

    }


    public void ajaxGetBlackAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String enamess = request.getParameter("enamess");
        BeQuery beQuery = new BeQuery();
        beQuery.setEnamess(enamess);
        Gson gson = new Gson();
        String str = gson.toJson(getPageHelper(request, response, beQuery));
        response.getWriter().println(str);
    }


    public void ajaxquery(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String enamess = request.getParameter("enamess");

        BeQuery beQuery = new BeQuery();
        beQuery.setEnamess(enamess);
        PageHelper<BlackEnterprise> pageHelper = getPageHelper(request, response, beQuery);
        Gson gson=new Gson();
        response.getWriter().println(gson.toJson(pageHelper));

    }






    //通用方法,参数有request,response,query
    public PageHelper<BlackEnterprise> getPageHelper(HttpServletRequest request, HttpServletResponse response, BeQuery beQuery) throws IOException{
        String account = request.getParameter("account");
        String password = request.getParameter("password");

        UserEnterprise user = userService.select(account,password);

        int id = user.getEnterprise_id();
        int indexpage=1;
        if(request.getParameter("indexPage")!=null){
            indexpage=Integer.parseInt(request.getParameter("indexPage"));
        }
        PageHelper<BlackEnterprise> ph=new PageHelper<>();
        ph.setIndexPage(indexpage);
        ArrayList<BlackEnterprise> list=blackListService.findIndexPageData(ph,beQuery,id);
        ph.setList(list);
        ph.setTotalData(blackListService.countTotalData(beQuery,id));
        return ph;
    }



    //获得供应商信息
    public void findSupId(HttpServletRequest request, HttpServletResponse response) throws IOException {
        EnterpriseService es = new EnterpriseServiceImpl();
        BlackListService bs = new BlackListServiceImpl();

        int eid = Integer.parseInt(request.getParameter("eid"));

        ArrayList<EnterpriseName> list1 = es.selectAllName(eid);
        ArrayList<BlackSupName> list2 = bs.selectBlackSupName(eid);
        ArrayList<WhiteSupName> list = whiterListService.selectWhiteSupName(eid);
        for(int i =0;i<list.size();i++){
            for(int j=0;j<list1.size();j++){
                if(list1.get(j).getEnamess().equals(list.get(i).getEnamess())){
                    list1.remove(list1.get(j));
                }
            }
        }
        for(int n=0;n<list2.size();n++){
            for(int m=0;m<list1.size();m++){
                if(list1.get(m).getEnamess().equals(list2.get(n).getEnamess())){
                    list1.remove(list1.get(m));
                }
            }
        }
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(list1));
    }


    public void findCodeByName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String enamess = request.getParameter("enamess");
        Enterprise enterprise = blackListService.selectCodeByName(enamess);
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(enterprise.getSocial_uniform_code()));

    }

    public void addSup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String enamess = request.getParameter("enamess"); //供货商的

        String localename = request.getParameter("localename");

        String str = request.getParameter("str");

        //通过名字找id 然后添加
        Enterprise e1 =  enterpriseService.selectAll(enamess);
        int id1 = e1.getEid();  //供货商的ID

        Enterprise e2 = enterpriseService.selectAll(localename);
        int id2 = e2.getEid();   //当前企业的ID

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //实例化对象
        Date d = new Date();
        //日期转为字符串
        String time = sdf.format(d);

        Boolean boo = true;
        if(blackListService.addSup(id2,id1,str,time)){
            boo = true;
        }else{
            boo = false;
        }
        response.getWriter().println(boo);
    }


    public void blackDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String enamess = request.getParameter("enamess"); //供货商的

        String localename = request.getParameter("localename");
        //通过名字找id
        Enterprise e1 =  enterpriseService.selectAll(enamess);
        int id1 = e1.getEid();  //供货商的ID

        Enterprise e2 = enterpriseService.selectAll(localename);
        int id2 = e2.getEid();   //当前企业的ID

        boolean boo1 = blackListService.delSup(id2,id1);

        response.getWriter().println(boo1);


    }








}
